//
//  ADDSHDZViewController.m
//  YDD
//
//  Created by 欧洪飞 on 2017/8/22.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "ADDSHDZViewController.h"

#import "MapSelPlaceViewController.h"
@interface ADDSHDZViewController ()<MapSelPlaceViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textName;
@property (weak, nonatomic) IBOutlet UIButton *btnMan;
@property (weak, nonatomic) IBOutlet UIButton *btnWoman;
@property (weak, nonatomic) IBOutlet UITextField *text2;
@property (weak, nonatomic) IBOutlet UILabel *labPlace;
@property (weak, nonatomic) IBOutlet UITextField *textMeng;

@property (nonatomic,copy)NSString *sex;//
@property (nonatomic,copy)NSString *province;
@property (nonatomic,copy)NSString *city;
@property (nonatomic,copy)NSString *area;

@property (nonatomic,copy)NSString *lat;
@property (nonatomic,copy)NSString *lng;


@end

@implementation ADDSHDZViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self showNav:@"添加地址"];
    
    _btnMan.layer.borderWidth = 0.5;
    _btnMan.layer.cornerRadius = 5.0;
    _btnMan.layer.borderColor = [UIColor grayColor].CGColor;
    _btnMan.backgroundColor = [UIColor redColor];
    [_btnMan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    _btnWoman.layer.borderWidth = 0.5;
    _btnWoman.layer.cornerRadius = 5.0;
    _btnWoman.layer.borderColor = [UIColor grayColor].CGColor;
    
    _sex = @"2";
    
    UITapGestureRecognizer *regionGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(regionEvent)];
    self.labPlace.userInteractionEnabled = YES;
    [self.labPlace addGestureRecognizer:regionGest];
}
//地图
- (void)regionEvent{
    MapSelPlaceViewController *map = [[MapSelPlaceViewController alloc] init];
    map.delegate = self;
    [self.navigationController pushViewController:map animated:YES];
}
#pragma -mark MapSelPlaceViewControllerDelegate
-(void)sendText:(NSString *)text Province:(NSString *)proce City:(NSString *)city Area:(NSString *)area{
  self.labPlace.text = text;
    _province = proce;
    _city = city;
    _area = area;
}

-(void)sendAddress:(NSString *)address Det:(NSString *)det{
    self.labPlace.text = [NSString stringWithFormat:@"%@%@",det,address];
}

-(void)sendLat:(NSString *)lat sendLng:(NSString *)lng{
    _lat = lat;
    _lng = lng;
}


- (IBAction)clickMan:(id)sender {
    _btnMan.backgroundColor = [UIColor redColor];
    [_btnMan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    _btnWoman.backgroundColor = [UIColor whiteColor];
    [_btnWoman setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    _sex = @"2";
}

- (IBAction)clickWoman:(id)sender {
    _btnMan.backgroundColor = [UIColor whiteColor];
    [_btnMan setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    
    _btnWoman.backgroundColor = [UIColor redColor];
    [_btnWoman setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _sex = @"3";
}
//添加收货地址
- (IBAction)clickUpdate:(id)sender {
    if (_textName.text.length == 0) {
        [LICGToast showWithText:@"联系人姓名不能为空"];
        return;
    }else if ([self validateMobile:_text2.text] == 0){
        [LICGToast showWithText:@"手机号格式不正确"];
        return;
    }else if (_textMeng.text.length == 0){
        [LICGToast showWithText:@"请输入详细地址"];
        return;
    }
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:token forKey:@"verifyToken"];
    [parmas setValue:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setValue:_textName.text forKey:@"consignee"];
    [parmas setValue:_sex forKey:@"sex"];
    [parmas setValue:_text2.text forKey:@"userphone"];
    [parmas setValue:_province forKey:@"province"];
    [parmas setValue:_city forKey:@"city"];
    [parmas setValue:_area forKey:@"area"];
    [parmas setValue:[NSString stringWithFormat:@"%@,%@",_labPlace.text,_textMeng.text]  forKey:@"detailedadd"];
    [parmas setValue:_lng forKey:@"longitude"];
    [parmas setValue:_lat forKey:@"latitude"];
    NSLog(@"parmas:%@",parmas);
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/addUserAddress",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"添加收货地址res:%@",responseObject);
        NSInteger status = [[responseObject objectForKey:@"status"] integerValue];
        NSString *msg = [responseObject objectForKey:@"msg"];
        if (status == 200) {

            [LICGToast showWithText:msg];
            [self.navigationController popViewControllerAnimated:YES];
            [self.delegate reloadDataList];
        }else{
            
            [LICGToast showWithText:msg];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD dismiss];
        NSLog(@"error:%@",error);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
